package org.tio.freemarker;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.utils.freemarker.FreemarkerUtils;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import jodd.io.FileUtil;

/**
 * @author tanyaowu 
 * 2017年11月13日 下午3:33:54
 */
public class Main {
	private static Logger log = LoggerFactory.getLogger(Main.class);

	/**
	 * 
	 * @author tanyaowu
	 */
	public Main() {
	}

	/**
	 * @param args
	 * @author tanyaowu
	 * @throws IOException 
	 */
	public static void main(String[] args) throws Exception {
		
//		System.getProperty("src");
//		System.getProperty("target");
		String src = System.getProperty("src");
		String target = System.getProperty("target");
		
		File srcDir = new File(src);
		File targetDir = new File(target);
		
		FileUtil.copy(targetDir.getCanonicalPath(), targetDir.getCanonicalPath() + "_back");
		
		FileUtil.cleanDir(targetDir);
		
		
		Configuration cfg = new Configuration(Configuration.getVersion());

		// Specify the source where the template files come from. Here I set a
		// plain directory for it, but non-file-system sources are possible too:
		cfg.setDirectoryForTemplateLoading(srcDir);

		// Set the preferred charset template files are stored in. UTF-8 is
		// a good choice in most applications:
		cfg.setDefaultEncoding("UTF-8");

		// Sets how errors will appear.
		// During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

		// Don't log exceptions inside FreeMarker that it will thrown at you anyway:
		cfg.setLogTemplateExceptions(false);

		// Wrap unchecked exceptions thrown during template processing into TemplateException-s.
		cfg.setWrapUncheckedExceptions(true);
		
		traverseFolder2(srcDir, targetDir, srcDir, cfg);
		
		
	}
	
	public static void traverseFolder2(File srcRoot, File targetRoot, File currFile, Configuration cfg) throws IOException, TemplateException {
        if (currFile.exists()) {
            File[] files = currFile.listFiles();
            if (files.length == 0) {
                log.info("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                    	log.info("文件夹:" + file2.getCanonicalPath());
                        traverseFolder2(srcRoot, targetRoot, file2, cfg);
                    } else {
                    	log.info("文件:" + file2.getCanonicalPath());
                    	String templateFilePath = file2.getCanonicalPath().substring(srcRoot.getCanonicalPath().length());
                    	try {
							FreemarkerUtils.generateFileByFile(templateFilePath, new File(targetRoot, templateFilePath).getCanonicalPath(), cfg, null);
						} catch (Exception e) {
							FileUtil.copy(file2.getCanonicalPath(), new File(targetRoot, templateFilePath).getCanonicalPath());
						}
                    }
                }
            }
        } else {
        	log.info("文件不存在!");
        }
    }
	
	public static void generate(Configuration cfg) {
		
	}
}
